package com.newegg.app.qec.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.newegg.app.qec.R;
import com.newegg.app.qec.util.APIMethod;
import com.newegg.app.qec.util.BaseActivity;
import com.newegg.app.qec.util.Constants;
import com.newegg.app.qec.util.MyApplication;
import com.newegg.app.qec.util.NetworkUtil;
import com.newegg.app.qec.util.ToastUtil;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Allen on 2017/12/7.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    //注册、忘记密码
    private TextView register, forget;
    //是否显示密码
    private ImageView open_close;
    //记录是否显示密码点击状态
    private boolean lean = false;
    private EditText number_et, password_et;
    private Button login_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        register = (TextView) findViewById(R.id.register_tv);
        forget = (TextView) findViewById(R.id.forget_tv);
        open_close = (ImageView) findViewById(R.id.open_close);
        number_et = (EditText) findViewById(R.id.number_et);
        password_et = (EditText) findViewById(R.id.password_et);
        login_btn = (Button) findViewById(R.id.login_btn);

        register.setOnClickListener(this);
        forget.setOnClickListener(this);
        open_close.setOnClickListener(this);
        login_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.open_close:
                if (!lean) {
                    lean = true;
                    open_close.setImageResource(R.mipmap.open_password);
                    password_et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    lean = false;
                    open_close.setImageResource(R.mipmap.close_password);
                    password_et.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                password_et.setSelection(password_et.getText().length());
                break;
            case R.id.register_tv:
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                intent.putExtra("tag", "register");
                startActivity(intent);
                break;
            case R.id.forget_tv:
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                intent.putExtra("tag", "forget");
                startActivity(intent);
                break;
            case R.id.login_btn:
                String number = number_et.getText().toString();
                String password = password_et.getText().toString();
                if (!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)) {
                    if (NetworkUtil.isNetworkAvailable(LoginActivity.this)) {
                        login(number, password);
                    } else {
                        ToastUtil.showToast(LoginActivity.this, "请检查您的网络状态");
                    }
                    login(number, password);
                } else {
                    ToastUtil.showToast(LoginActivity.this, "请输入账号和密码");
                }

                break;
        }
    }

    private void login(final String number, final String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    RequestBody formBody = new FormBody.Builder().add("grant_type", "password").
                            add("username", number).add("password", password).build();
                    Request request = new Request.Builder().url(APIMethod.Login).post(formBody).build();
                    Response response = MyApplication.okHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    Log.i("login", code + "");
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.obj = bodys;
                        handler.sendMessage(msg1);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    //存入是否成功登录状态，方便其他页面判断,"LoginState"
                    Constants.setLoginByName(LoginActivity.this, "LoginState", true);
                    //发送广播更新我的页面数据
                    sendBroadcast(new Intent("QEC").putExtra("QEC", 0));
                    LoginActivity.this.finish();
                    break;
                case 1:
                    ToastUtil.showToast(LoginActivity.this, "登录失败");
                    break;
            }
        }
    };
}
